//
//  DetailsViewController.m
//  GoldOperationClient
//
//  Created by yaxue wang on 16/8/11.
//  Copyright © 2016年 展鸿科技. All rights reserved.
//

#import "DetailsViewController.h"
#import "ProgressView.h"
#import "HistoryViewController.h"
#import "ProgressModel.h"
#import "MWPhotoBrowser.h"
#import "LocationViewController.h"
#import "MapDetailViewController.h"
#import "SegmentView.h"
#import "ReplyViewController.h"
#import "DetailTable.h"
#import "CInputBoxView.h"
#import "SuspensionView.h"
#import "ScanViewController.h"
#import <BaiduMapAPI_Utils/BMKUtilsComponent.h>
#import <BaiduMapAPI_Map/BMKMapComponent.h>
#import <BaiduMapAPI_Search/BMKSearchComponent.h>
#import <BaiduMapAPI_Map/BMKMapComponent.h>
#import <BaiduMapAPI_Location/BMKLocationComponent.h>
#import "CompleteTypeView.h"
#import "ShareViewController.h"
#import "LocationViewController.h"
#import "SampleViewController.h"
#import "SampleDetailViewController.h"
#import "SampleCompleteViewController.h"
#import "TimeView.h"
#import "LoadingView.h"
#import "CompleteViewController.h"
@interface DetailsViewController()<MWPhotoBrowserDelegate,BMKMapViewDelegate,BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate>
{
    BMKLocationService* _locService;
    BMKGeoCodeSearch *_geocodesearch;
    BOOL isGeoSearch;
    //发送本地定位
    double longitude;
    double latitude;
    BOOL isAddInfo;
    
}
@property(nonatomic) BOOL isCompletceClick;
@property(nonatomic) BOOL isRemoteTake;
@property(nonatomic) BOOL isTake;
@property(nonatomic,strong)UIView *proView;
@property(nonatomic,strong) NSMutableArray *photosAr;
@property(nonatomic,strong) DetailTable *detailTable;
@property(nonatomic,strong) UIView *detailView;
@property(nonatomic,strong)AVAudioPlayer *audioPlayer;
@property(nonatomic,strong)NSString *messageNum;
@property(nonatomic,strong)NSString *addType;
@property(nonatomic,strong)ProgressView* progressView;
@end
@implementation DetailsViewController
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *playPath = [NSString stringWithFormat:@"%@/%@.mp3", docDirPath , @"play"];
    
    if([fileManager removeItemAtPath:playPath error:nil])
    {
        echo(@"删除以前的mp3文件");
    }
    self.isCompletceClick=NO;

    
    
}

-(void)viewDidAppear:(BOOL)animated{
    
   [self.detailTable willDetailView];
    self.isCompletceClick=NO;
    [_progressView refresh];

    //yes 要补充设备，no不需要
    [CClass HttpRequestTask:YES withUrl:@"warninglist_orderContent" withData:@{@"warningUuid":self.warningUuid} notifyError:YES onSuccess:^(BaseModel *model) {
        NSDictionary *dataDic=model.B_data;
        if ([F_NSpace([dataDic objectForKey:@"machineName"]) isEqualToString:@""]||[F_NSpace([dataDic objectForKey:@"typeNm"]) isEqualToString:@""]) {
            isAddInfo=YES;
            //没有名字和类型
            if ([F_NSpace([dataDic objectForKey:@"machineName"]) isEqualToString:@""]&&[F_NSpace([dataDic objectForKey:@"typeNm"]) isEqualToString:@""]) {
                self.addType=@"请先补充报修设备！";
            }else
            {
                self.addType=@"请对报修设备补充设备信息！";

            }
            
        }else
        {
            isAddInfo=NO;
        }
        
        
    } onError:^(BaseModel *model) {
        
    }];

   
    
}
- (void)initView
{
    [super initView];
    self.isRemoteTake=YES;
    self.isTake=YES;
    self.isCompletceClick=NO;
    _photosAr=[[NSMutableArray alloc]init];
    //初始化BMKLocationService
    _locService = [[BMKLocationService alloc]init];
    _locService.delegate = self;
    //启动LocationService
    [_locService startUserLocationService];
    _geocodesearch = [[BMKGeoCodeSearch alloc] init];
    //编码服务的初始化(就是获取经纬度,或者获取地理位置服务)
    _geocodesearch.delegate = self;//设置代理为self
    
    self.navBar.hidden=NO;
    __block typeof(self) blockself = self;

    NSInteger num =[self.state integerValue];
    UIView *narView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, 64)];
    narView.backgroundColor =[UIColor whiteColor];
    [self.view addSubview:narView];
    [narView resignFirstResponder];
    UIView *backView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 64)];
    [narView addSubview:backView];
    [backView bk_whenTapped:^{
        [blockself backController];
        [blockself.detailTable stopAudioPlayer];
        
    }];
    
    UIImage *icoBack = [UIImage imageNamed:@"Ico_nar_back"];
    UIImageView *imgBack = [[UIImageView alloc] initWithFrame:CGRectMake(15, 33, icoBack.size.width, icoBack.size.height)];
    imgBack.image=icoBack;
    [backView addSubview:imgBack];
    
    UILabel *backTitle = [[UILabel alloc] init];
    backTitle.frame=CGRectMake(CGRectGetMaxX(imgBack.frame) + 5, 20, 50 - imgBack.frame.size.width,44);
    
    [backTitle setText:@"返回"];
    [backTitle setTextColor:MainColor];
    [backTitle setFont:F(14)];
    [backView addSubview:backTitle];
    UIImage *shareiImage=[UIImage imageNamed:@"Ico_detail_share"];
    UIImage *addImage=[UIImage imageNamed:@"Ico_detail_add"];

    UIView *ringView=[[UIView alloc]initWithFrame:CGRectMake(WindowWidth-shareiImage.size.width-addImage.size.width-40, 24, shareiImage.size.width+addImage.size.width+45, 40)];
    [narView addSubview:ringView];
    
    UIView *addView =[[UIView alloc]initWithFrame:CGRectMake(ringView.bounds.size.width-addImage.size.width-20, 0, addImage.size.width+20, 40)];
    [ringView addSubview:addView];

    UIImageView *addImg=[[UIImageView alloc]initWithFrame:CGRectMake(0, (40-addImage.size.height)/2, addImage.size.width, addImage.size.height)];
    addImg.userInteractionEnabled=YES;
    addImg.image=addImage;
    [addView addSubview:addImg];
   //分享
    UIView *shareView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, shareiImage.size.width+15, 40)];
    [ringView addSubview:shareView];
    [shareView bk_whenTapped:^{
        ShareViewController *shareVC=[[ShareViewController alloc]init];
        shareVC.warningUuid=self.warningUuid;
        [self pushController:shareVC];
    }];
    UIImageView *shareImg=[[UIImageView alloc]initWithFrame:CGRectMake(0, (40-shareiImage.size.height)/2, shareiImage.size.width, shareiImage.size.height)];
    shareImg.userInteractionEnabled=YES;
    shareImg.image=shareiImage;
    [shareView addSubview:shareImg];
    
    UIView *topView =[[UIView alloc]init];
    topView.frame=CGRectMake((WindowWidth-140)/2, 30, 140, 25);
    
    topView.layer.masksToBounds=YES;
    topView.layer.cornerRadius=4;
    topView.backgroundColor =C(@"ebe9e5");
    [narView addSubview:topView];
    UIButton *btn1=[UIButton buttonWithType:UIButtonTypeCustom];
    btn1.frame =CGRectMake(0, 0, topView.bounds.size.width/2,  topView.bounds.size.height);
    [btn1 setTitle:@"进度" forState:UIControlStateNormal];
    btn1.titleLabel.font =F(14);
    [topView addSubview:btn1];
    [btn1 setBackgroundColor:MainColor];
    [btn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    UIButton *btn2=[UIButton buttonWithType:UIButtonTypeCustom];
    btn2.frame =CGRectMake(topView.bounds.size.width/2, 0, topView.bounds.size.width/2,  topView.bounds.size.height);
    [btn2 setTitle:@"详情" forState:UIControlStateNormal];
    btn2.titleLabel.font =F(14);
    [topView addSubview:btn2];
    [btn2 setBackgroundColor:C(@"ebe9e5")];
    [btn2 setTitleColor:C(@"a7a6a6") forState:UIControlStateNormal];
   
    UIView *bottomLineView = [[UIView alloc] initWithFrame:CGRectMake(0, narView.bounds.size.height-.5, [UIScreen mainScreen].bounds.size.width, .5)];
    [bottomLineView setBackgroundColor:LineColor];
    [narView addSubview:bottomLineView];

    BOOL isHidden; //num从前面传来的
    if (num==97||num==99||num==98||num==1) {
        //隐藏补充按钮 待接单、已撤销、待评价、已完成的报修单无法回复 隐藏加号
        isHidden=YES;
       
    }else
    {
        //不隐藏补充按钮
        isHidden=NO;
       
    }
    BOOL isHiddenAdd; //num从前面传来的  隐藏补充设备的功能 已撤销、待评价、已完成

    if (num==97||num==99||num==98) {
        isHiddenAdd=YES;
        shareView.frame =CGRectMake(ringView.bounds.size.width-shareiImage.size.width-20, 0, shareiImage.size.width+20, 40);
        addView.frame =CGRectMake(0, 0, 0, 0);
        addView.hidden=YES;
  
        
    }else
    {
        //不隐藏补充设备按钮
        isHiddenAdd=NO;
        shareView.frame =CGRectMake(0, 0, shareiImage.size.width+15, 40);
        addView.frame =CGRectMake(ringView.bounds.size.width-addImage.size.width-20, 0, addImage.size.width+20, 40);
        addView.hidden=NO;
        
    }
    
    self.detailView=[[UIView alloc]initWithFrame:CGRectMake(0, 74,WindowWidth,  WindowHeight-74)];
    [self.view addSubview:self.detailView];

    self.detailTable =[[DetailTable alloc]initWithFrame:CGRectMake(0, 0,WindowWidth,  self.detailView.bounds.size.height) withWarningUuid:self.warningUuid withState:isHidden withIsAdd:isHiddenAdd];
    [self.detailView addSubview: self.detailTable];

    _proView=[[UIView alloc]initWithFrame:CGRectMake(0, 74, WindowWidth, WindowHeight-74)];
    [self.view addSubview:_proView];
    _proView.backgroundColor =[UIColor clearColor];
   _progressView=[[ProgressView alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, _proView.frame.size.height-10)withWarningUuid:self.warningUuid];
    
    [_proView addSubview:_progressView];
  
    __block typeof (self)blockSelf=self;

#pragma mark 进入采购状态界面
    [_progressView setGotoSampleState:^(NSMutableDictionary *dic) {
        SampleCompleteViewController *sampleVc=[[SampleCompleteViewController alloc]init];
        sampleVc.orderUuid=[dic objectForKey:@"warning_uuid"];
        sampleVc.purchaseuuid=[dic objectForKey:@"purchase_uuid"];
        [blockSelf pushController:sampleVc];
    }];
    UIView *bottomView =[[UIView alloc]initWithFrame:CGRectMake(0, WindowHeight-70, WindowWidth, 70)];
    bottomView.backgroundColor=BackColor;
    [self.view addSubview:bottomView];
    
    if (self.type==1) {
        self.detailView.hidden=YES;
        _proView.hidden=NO;
        bottomView.hidden=NO;
        [btn1 setBackgroundColor:MainColor];
        [btn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        btn2.backgroundColor=C(@"e8e8e8");
        [btn2 setTitleColor:C(@"5a5a5a") forState:UIControlStateNormal];
        if (num==1||num==2||num==4||num==5||num==6) {
            _proView.frame=CGRectMake(0, 74, WindowWidth, WindowHeight-74-70);
            _progressView.frame=CGRectMake(0, 0, WindowWidth, _proView.frame.size.height);
            bottomView.hidden=NO;
            
            
            
        }else
        {
            _proView.frame=CGRectMake(0, 74, WindowWidth, WindowHeight-74);
            _progressView.frame=CGRectMake(0, 0, WindowWidth, _proView.frame.size.height);
            bottomView.hidden=YES;
            
            
        }
    }else if(self.type==2)
    {
        bottomView.hidden=YES;
        self.detailView.hidden=NO;
        _proView.hidden=YES;
        [btn2 setBackgroundColor:MainColor];
        [btn2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        btn1.backgroundColor=C(@"e8e8e8");
        [btn1 setTitleColor:C(@"5a5a5a") forState:UIControlStateNormal];
    }


    [btn1 bk_addEventHandler:^(id sender) {
        if (num==1||num==2||num==4||num==5||num==6) {//num从前面传来的
            bottomView.hidden=NO;
            
        }else
        {
            bottomView.hidden=YES;
            
        }
        [self.detailTable stopAudioPlayer];
        self.detailView.hidden=YES;
        _proView.hidden=NO;
        [btn1 setBackgroundColor:MainColor];
        [btn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        btn2.backgroundColor=C(@"e8e8e8");
        [btn2 setTitleColor:C(@"5a5a5a") forState:UIControlStateNormal];
    } forControlEvents:UIControlEventTouchUpInside];
    
    [btn2 bk_addEventHandler:^(id sender) {
        [self.detailTable stopAudioPlayer];
        bottomView.hidden=YES;
        self.detailView.hidden=NO;
        _proView.hidden=YES;
        [btn2 setBackgroundColor:MainColor];
        [btn2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        btn1.backgroundColor=C(@"e8e8e8");
        [btn1 setTitleColor:C(@"5a5a5a") forState:UIControlStateNormal];
        
        
    } forControlEvents:UIControlEventTouchUpInside];
    
    
    UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
    [bottomView addSubview:button];
    button.frame=CGRectMake(29, (70-40)/2, WindowWidth-58, 40);
    [button setBackgroundColor:MainColor];
    button.layer.masksToBounds=YES;
    button.layer.cornerRadius=8;
    
    UIButton *remoteBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    [bottomView addSubview:remoteBtn];
    [remoteBtn setBackgroundColor:MainColor];
    remoteBtn.layer.masksToBounds=YES;
    remoteBtn.layer.cornerRadius=8;
    remoteBtn.hidden=YES;
   
    
    if (num==0) {
        //待分配 0 不可能出现
        
    }else if(num==1)
    {
        //  待接单 1
        //转介  接单
        [button setTitle:@"接单" forState:UIControlStateNormal];
        [button bk_addEventHandler:^(id sender) {
            NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
            [dic setObject:self.warningUuid forKey:@"orderUuid"];
            [dic setObject:[NSNumber numberWithDouble:longitude] forKey:@"longitude"];
            [dic setObject:[NSNumber numberWithDouble:latitude] forKey:@"latitude"];
            
            [TimeView showUuid:[dic objectForKey:@"orderUuid"] longitude:[dic objectForKey:@"longitude"] latitude:[dic objectForKey:@"latitude"] action:^(NSInteger index) {
                    [self backController];

            }];

           
            
        } forControlEvents:UIControlEventTouchUpInside];
        [addView bk_whenTapped:^{
            NSArray *temp = @[@{@"image" : @"Ico_ referral", @"title" : @"转介"}];
            [SuspensionView showInputBoxbtns:temp action:^(SuspensionView *boxView, NSInteger index) {
                if (index==0) {
                    //转介
                    [CInputBoxView showInputBox:@"转介" place:@"转介原因、转介对象" show:@"请填写转介原因！" btns:@[@"转介",@"取消"] action:^(CInputBoxView *boxView, NSInteger index) {
                        if (index==0) {
                            [self setIsLoading:YES];
                            
                            NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
                            [dic setObject:self.warningUuid forKey:@"orderUuid"];
                            [dic setObject:[NSNumber numberWithDouble:longitude] forKey:@"longitude"];
                            [dic setObject:[NSNumber numberWithDouble:latitude] forKey:@"latitude"];
                            [dic setObject:boxView.inputData forKey:@"content"];
                            [CClass HttpRequestTask:YES withUrl:@"warning_orderRefer" withData:dic notifyError:YES onSuccess:^(BaseModel *model) {
                                [CClass showLittleTimeMessage:@"转介成功" completion:^{
                                    [self backController];
                                    [self setIsLoading:NO];
                                    
                                }];
                                
                            } onError:^(BaseModel *model) {
                                [self setIsLoading:NO];

                            }];
                        }
                    }];
                }
                
            }];

        }];
      
        
    }else if(num==2)
    {
        //待处理 2
        //转介 采购  开始
        remoteBtn.hidden=NO;
        remoteBtn.frame=CGRectMake(29, (70-40)/2, (WindowWidth-58-15)/2, 40);
        [remoteBtn setTitle:@"远程处理" forState:UIControlStateNormal];
        [remoteBtn bk_addEventHandler:^(id sender) {
            
            if (self.isRemoteTake==YES) {
                self.isRemoteTake=NO;
                [self setIsLoading:YES];
                NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
                [dic setObject:self.warningUuid forKey:@"orderUuid"];
                [dic setObject:[NSNumber numberWithDouble:latitude]  forKey:@"latitude"];
                [dic setObject:[NSNumber numberWithDouble:longitude]  forKey:@"longitude"];
                [CClass HttpRequestTask:YES withUrl:@"warning_orderProcessByProRemote" withData:dic notifyError:YES onSuccess:^(BaseModel *model) {
                    [CClass showLittleTimeMessage:@"开始处理" completion:^{
                        [self backController];
                        [self setIsLoading:NO];

                    }];
                } onError:^(BaseModel *model) {
                    self.isRemoteTake=YES;
                    [self setIsLoading:NO];

                }];

            }
            
            
            
        } forControlEvents:UIControlEventTouchUpInside];
        button.frame=CGRectMake(CGRectGetMaxX(remoteBtn.frame)+15, (70-40)/2, (WindowWidth-58-15)/2, 40);
        [button setTitle:@"现场处理" forState:UIControlStateNormal];
        [button bk_addEventHandler:^(id sender) {
             if (self.isTake==YES) {
               [self setIsLoading:YES];
                 self.isTake=NO;

                NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
                [dic setObject:self.warningUuid forKey:@"orderUuid"];
                [dic setObject:[NSNumber numberWithDouble:latitude]  forKey:@"latitude"];
                [dic setObject:[NSNumber numberWithDouble:longitude]  forKey:@"longitude"];
                [CClass HttpRequestTask:YES withUrl:@"warning_orderProcessByPro" withData:dic notifyError:YES onSuccess:^(BaseModel *model) {
                    
                    [CClass showLittleTimeMessage:@"定位成功，开始处理" completion:^{
                        [self backController];
                        [self setIsLoading:NO];
                        
                    }];
                } onError:^(BaseModel *model) {
                    [self setIsLoading:NO];

                    if ([model.B_code isEqualToString:@"500"]) {
                        
                    }
                    self.isTake=YES;

                }];
   
            }
            
        } forControlEvents:UIControlEventTouchUpInside];
        [addView bk_whenTapped:^{
            NSArray *temp = @[@{@"image" : @"Ico_ referral", @"title" : @"转介"},@{@"image" : @"Ico_ referral", @"title" : @"采购"}];
            [SuspensionView showInputBoxbtns:temp action:^(SuspensionView *boxView, NSInteger index) {
                
                if (index==0) {
                    //转介
                    [CInputBoxView showInputBox:@"转介" place:@"转介原因、转介对象" show:@"请填写转介原因！" btns:@[@"转介",@"取消"] action:^(CInputBoxView *boxView, NSInteger index) {
                        if (index==0) {
                            [self setIsLoading:YES];
                            NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
                            [dic setObject:self.warningUuid forKey:@"orderUuid"];
                            [dic setObject:[NSNumber numberWithDouble:longitude] forKey:@"longitude"];
                            [dic setObject:[NSNumber numberWithDouble:latitude] forKey:@"latitude"];
                            [dic setObject:boxView.inputData forKey:@"content"];
                            
                            [CClass HttpRequestTask:YES withUrl:@"warning_orderRefer" withData:dic notifyError:YES onSuccess:^(BaseModel *model) {
                                [CClass showLittleTimeMessage:@"转介成功" completion:^{
                                    [self backController];
                                    [self setIsLoading:NO];
                                    
                                }];
                                
                            } onError:^(BaseModel *model) {
                                [self setIsLoading:NO];

                            }];
                        }
                    }];

                    
                }else if(index==1)
                {
                    //采购
                    SampleViewController *sampleVC=[[SampleViewController alloc]init];
                    sampleVC.orderUuid=self.warningUuid;
                    [self pushController:sampleVC];
                }
                
                
            }];
 
        }];
        
        
    }else if(num==4)
    {
        //处理中 4
        //转介  挂起  采购    完成
        [button setTitle:@"完成" forState:UIControlStateNormal];
        
        [button bk_addEventHandler:^(id sender) {
            if (isAddInfo==YES) {
                [CClass showMessage:self.addType];
                return;
            }
          
//            if (self.isCompletceClick==YES) {
//                
//            }else
//            {
               [[LoadingView shareLoading] startAnimating];
//                self.isCompletceClick=YES;
            [CompleteTypeView showTypeAr:nil Action:^(CompleteTypeView *boxView, NSInteger index) {
                
                if (index==1) {
                    NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
                    [dic setObject:self.warningUuid forKey:@"orderUuid"];
                    [dic setObject:boxView.typeData forKey:@"faultTypeUuid"];
                    [dic setObject:[NSNumber numberWithDouble:longitude] forKey:@"longitude"];
                    [dic setObject:[NSNumber numberWithDouble:latitude] forKey:@"latitude"];
                    //                          NSLog(@"=======%@",dic);
                    //完成
                    CompleteViewController*completeView=[[CompleteViewController alloc]init];
                    completeView.dataDic=dic;
                    [blockSelf pushController:completeView];
                    self.isCompletceClick=NO;
                    //                                    self.isCompletceClick=NO;
                    
                }
                [[LoadingView shareLoading] stopAnimating];
                
            }];

//                [CClass HttpRequestTask:YES withUrl:@"select_showFaultTypeSelect" withData:nil notifyError:YES onSuccess:^(BaseModel *model) {
//                    NSMutableArray*typeAr=(NSMutableArray*)model.B_data;
//
//                    [CompleteTypeView showTypeAr:typeAr Action:^(CompleteTypeView *boxView, NSInteger index) {
//
//                        if (index==1) {
//                            NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
//                            [dic setObject:self.warningUuid forKey:@"orderUuid"];
//                            [dic setObject:boxView.typeData forKey:@"faultTypeUuid"];
//                            [dic setObject:[NSNumber numberWithDouble:longitude] forKey:@"longitude"];
//                            [dic setObject:[NSNumber numberWithDouble:latitude] forKey:@"latitude"];
//                            //                          NSLog(@"=======%@",dic);
//                            //完成
//                            CompleteViewController*completeView=[[CompleteViewController alloc]init];
//                            completeView.dataDic=dic;
//                            [blockSelf pushController:completeView];
//                            [[LoadingView shareLoading] stopAnimating];
//                            self.isCompletceClick=NO;
//
//                        }
//                    }];
//                    
//                } onError:^(BaseModel *model) {
//                    
//                }];
// 
////            }
//
            

            
        } forControlEvents:UIControlEventTouchUpInside];
        [addView bk_whenTapped:^{
            NSArray *temp = @[@{@"image" : @"Ico_ referral", @"title" : @"转介"},
                              @{@"image" : @"Ico_ pending", @"title" : @"挂起"},@{@"image" : @"Ico_ pending", @"title" : @"采购"}];
            [SuspensionView showInputBoxbtns:temp action:^(SuspensionView *boxView, NSInteger index) {
                
                if (index==0) {
                    //转介
                    [CInputBoxView showInputBox:@"转介" place:@"转介原因、转介对象" show:@"请填写转介原因！" btns:@[@"转介",@"取消"] action:^(CInputBoxView *boxView, NSInteger index) {
                        if (index==0) {
                            [self setIsLoading:YES];
                            
                            NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
                            [dic setObject:self.warningUuid forKey:@"orderUuid"];
                            [dic setObject:[NSNumber numberWithDouble:longitude] forKey:@"longitude"];
                            [dic setObject:[NSNumber numberWithDouble:latitude] forKey:@"latitude"];
                            [dic setObject:boxView.inputData forKey:@"content"];
                            [CClass HttpRequestTask:YES withUrl:@"warning_orderRefer" withData:dic notifyError:YES onSuccess:^(BaseModel *model) {
                                [CClass showLittleTimeMessage:@"转介成功" completion:^{
                                    [self backController];
                                    [self setIsLoading:NO];
                                    
                                }];
                                
                            } onError:^(BaseModel *model) {
                                [self setIsLoading:NO];

                            }];
                        }
                    }];
                }else if (index==1)
                {
                    //挂起
                    [CInputBoxView showInputBox:@"挂起" place:@"挂起原因" show:@"请填写挂起原因" btns:@[@"挂起",@"取消"] action:^(CInputBoxView *boxView, NSInteger index) {
                        if (index==0) {
                            [self setIsLoading:YES];
                            
                            NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
                            [dic setObject:self.warningUuid forKey:@"orderUuid"];
                            [dic setObject:[NSNumber numberWithDouble:longitude] forKey:@"longitude"];
                            [dic setObject:[NSNumber numberWithDouble:latitude] forKey:@"latitude"];
                            [dic setObject:boxView.inputData forKey:@"content"];
                            [CClass HttpRequestTask:YES withUrl:@"warning_orderSuspend" withData:dic notifyError:YES onSuccess:^(BaseModel *model) {
                                [CClass showLittleTimeMessage:@"挂起申请已提交" completion:^{
                                    [self backController];
                                    [self setIsLoading:NO];
                                    
                                }];
                                
                            } onError:^(BaseModel *model) {
                                [self setIsLoading:NO];

                            }];
                        }
                    }];
                }else if (index==2)
                {
                    //采购
                    SampleViewController *sampleVC=[[SampleViewController alloc]init];
                    sampleVC.orderUuid=self.warningUuid;
                    [self pushController:sampleVC];
                }
                
                
            }];
 
        }];
        
        
    }else if(num==5)
    {
        //待挂起  5
        //转介 采购   完成
        [button setTitle:@"完成" forState:UIControlStateNormal];
        
        [button bk_addEventHandler:^(id sender) {
            if (isAddInfo==YES) {
                [CClass showMessage:@"请对报修设备补充设备信息！"];
                return;
            }
            [CClass HttpRequestTask:YES withUrl:@"select_showFaultTypeSelect" withData:nil notifyError:YES onSuccess:^(BaseModel *model) {
                [[LoadingView shareLoading] startAnimating];
                NSMutableArray*typeAr=(NSMutableArray*)model.B_data;
                [CompleteTypeView showTypeAr:typeAr Action:^(CompleteTypeView *boxView, NSInteger index) {
                    
                    if (index==1) {
                        NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
                        [dic setObject:self.warningUuid forKey:@"orderUuid"];
                        [dic setObject:boxView.typeData forKey:@"faultTypeUuid"];
                        [dic setObject:[NSNumber numberWithDouble:longitude] forKey:@"longitude"];
                        [dic setObject:[NSNumber numberWithDouble:latitude] forKey:@"latitude"];
                        //                          NSLog(@"=======%@",dic);
                        //完成
                        CompleteViewController*completeView=[[CompleteViewController alloc]init];
                        completeView.dataDic=dic;
                        [blockSelf pushController:completeView];
                    }
                    [[LoadingView shareLoading] stopAnimating];
                }];
                
            } onError:^(BaseModel *model) {
                [[LoadingView shareLoading] stopAnimating];


            }];

//            [CompleteView showInputBox:nil place:@"维护备注"  show:@"请填写维护备注！" showType:@"请选择故障类型！"  btns:@[@"完成",@"取消"] action:^(CompleteView *boxView, NSInteger index) {
//                if (index==0) {
//                    [self setIsLoading:YES];
//                    NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
//                    [dic setObject:self.warningUuid forKey:@"orderUuid"];
//                    [dic setObject:boxView.inputData forKey:@"content"];
//                    [dic setObject:boxView.typeData forKey:@"faultTypeUuid"];
//                    [dic setObject:[NSNumber numberWithDouble:longitude] forKey:@"longitude"];
//                    [dic setObject:[NSNumber numberWithDouble:latitude] forKey:@"latitude"];
//                    [CClass HttpRequestTask:YES withUrl:@"warning_orderCompleted" withData:dic notifyError:YES onSuccess:^(BaseModel *model) {
//                        [CClass showLittleTimeMessage:@"已完成" completion:^{
//                            [self backController];
//                            [self setIsLoading:NO];
//                            
//                        }];
//                        
//                    } onError:^(BaseModel *model) {
//                        
//                    }];
//                }
//            }];
            
        } forControlEvents:UIControlEventTouchUpInside];
        [addView bk_whenTapped:^{
            NSArray *temp = @[@{@"image" : @"Ico_ referral", @"title" : @"转介"},@{@"image" : @"Ico_ referral", @"title" : @"采购"}];
            [SuspensionView showInputBoxbtns:temp action:^(SuspensionView *boxView, NSInteger index) {
                if (index==0) {
                    //转介
                    [CInputBoxView showInputBox:@"转介" place:@"转介原因、转介对象" show:@"请填写转介原因！" btns:@[@"转介",@"取消"] action:^(CInputBoxView *boxView, NSInteger index) {
                        if (index==0) {
                            [self setIsLoading:YES];
                            NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
                            [dic setObject:self.warningUuid forKey:@"orderUuid"];
                            [dic setObject:[NSNumber numberWithDouble:longitude] forKey:@"longitude"];
                            [dic setObject:[NSNumber numberWithDouble:latitude] forKey:@"latitude"];
                            [dic setObject:boxView.inputData forKey:@"content"];
                            [CClass HttpRequestTask:YES withUrl:@"warning_orderRefer" withData:dic notifyError:YES onSuccess:^(BaseModel *model) {
                                [CClass showLittleTimeMessage:@"转介成功" completion:^{
                                    [self backController];
                                    [self setIsLoading:NO];
                                    
                                }];
                                
                            } onError:^(BaseModel *model) {
                                [self setIsLoading:NO];

                            }];
                        }else if (index==1)
                        {
                            //采购
                            SampleViewController *sampleVC=[[SampleViewController alloc]init];
                            sampleVC.orderUuid=self.warningUuid;
                            [self pushController:sampleVC];
                        }
                    }];
                }
                
            }];

        }];
        
    }else if(num==6)
    {
        //已挂起 挂起中 恢复  6
        //转介 //采购   恢复
        [button setTitle:@"恢复" forState:UIControlStateNormal];
        [button bk_addEventHandler:^(id sender) {
            [CClass showAlertTitle:nil hidden:NO context:@"确认恢复？" oneBtn:@"确定"twoBtn:@"取消" handler:^{
                [self setIsLoading:YES];
                NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
                [dic setObject:self.warningUuid forKey:@"orderUuid"];
                [dic setObject:[NSNumber numberWithDouble:longitude] forKey:@"longitude"];
                [dic setObject:[NSNumber numberWithDouble:latitude] forKey:@"latitude"];
                [CClass HttpRequestTask:YES withUrl:@"warning_orderRecoveryByPro" withData:dic notifyError:YES onSuccess:^(BaseModel *model) {
                    [CClass showLittleTimeMessage:@"开始处理" completion:^{
                        [self backController];
                        [self setIsLoading:NO];
                    }];
                    
                } onError:^(BaseModel *model) {
                    [self setIsLoading:NO];

                }];

            } handler:^{
                
            }];
            
        } forControlEvents:UIControlEventTouchUpInside];
        [addView bk_whenTapped:^{
            NSArray *temp = @[@{@"image" : @"Ico_ referral", @"title" : @"转介"},@{@"image" : @"Ico_ referral", @"title" : @"采购"}];
            [SuspensionView showInputBoxbtns:temp action:^(SuspensionView *boxView, NSInteger index) {
                if (index==0) {
                    //转介
                    [CInputBoxView showInputBox:@"转介" place:@"转介原因、转介对象" show:@"请填写转介原因！" btns:@[@"转介",@"取消"] action:^(CInputBoxView *boxView, NSInteger index) {
                        if (index==0) {
                            [self setIsLoading:YES];
                            NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
                            [dic setObject:self.warningUuid forKey:@"orderUuid"];
                            [dic setObject:[NSNumber numberWithDouble:longitude] forKey:@"longitude"];
                            [dic setObject:[NSNumber numberWithDouble:latitude] forKey:@"latitude"];
                            [dic setObject:boxView.inputData forKey:@"content"];
                            [CClass HttpRequestTask:YES withUrl:@"warning_orderRefer" withData:dic notifyError:YES onSuccess:^(BaseModel *model) {
                                [CClass showLittleTimeMessage:@"转介成功" completion:^{
                                    [self backController];
                                    [self setIsLoading:NO];
                                    
                                }];
                                
                            } onError:^(BaseModel *model) {
                                [self setIsLoading:NO];

                            }];
                        }else if (index==1)
                        {
                            //采购
                            SampleViewController *sampleVC=[[SampleViewController alloc]init];
                            sampleVC.orderUuid=self.warningUuid;
                            [self pushController:sampleVC];
                        }
                    }];
                }
                
            }];

        }];
        
    }
    

#pragma mark 机构进入地图
    [self.detailTable setGotoMap:^(NSMutableDictionary *dataDic) {
        LocationViewController *locationVC=[[LocationViewController alloc]init];
        locationVC.dataDic=dataDic;
        [blockSelf pushController:locationVC];
    }];
#pragma mark 设备记录和详情
    [self.detailTable setGotoDeviceDetail:^(NSString *machineUuid) {
      
        HistoryViewController *historyVC =[[HistoryViewController alloc]init];
        historyVC.machineUuid =machineUuid;
        historyVC.orderUuid=blockSelf.warningUuid;
        historyVC.num=num;
        historyVC.Type=MachineLink;//已经关联
        [blockSelf pushController:historyVC];
    }];
#pragma mark 设备扫码补充信息
    [self.detailTable setGotoAddDetail:^(NSString*warningUuid,NSString *instUuid) {
    ScanViewController *scanVC =[[ScanViewController alloc]init];
    [scanVC setDataType:HasListData];
    scanVC.num=num;
    scanVC.instUuid=instUuid;
    scanVC.orderUuid=warningUuid ;
    [blockself pushController:scanVC];

    }];
#pragma mark  进入采购明细界面
    [self.detailTable setGotoSampleDetail:^(NSString *warningUuid) {
        SampleDetailViewController *sampleVc=[[SampleDetailViewController alloc]init];
        sampleVc.warningUuid=blockSelf.warningUuid; //@"19296a316e544fbca3fb3bb79e8865c9";
        [blockSelf pushController:sampleVc];
    }];
#pragma mark 进入补充聊天界面
 [self.detailTable setGotoReply:^(NSString *warningUuid) {
        ReplyViewController *replyVC =[[ReplyViewController alloc]init];
        replyVC.orderUuid=  warningUuid;
        [blockSelf pushController:replyVC];
        
    }];
#pragma mark 视频和图片放大
    [self.detailTable setClickImg:^(NSMutableDictionary *imgDic) {
        [blockSelf.photosAr removeAllObjects];
        NSMutableArray *phoneAr=[imgDic objectForKey:@"phoneAr"];
        for (int i=0; i<phoneAr.count; i++) {
            NSDictionary *dic=[phoneAr objectAtIndex:i];
            NSInteger num =[[dic objectForKey:@"type"]integerValue];
            if (num==1) {
                [blockSelf.photosAr addObject:[MWPhoto photoWithURL:[dic objectForKey:@"img"]]];
            }else
            {
                [blockSelf.photosAr addObject:[MWPhoto videoWithURL:[dic objectForKey:@"img"]]];
                
            }
        }
        
        MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:blockSelf];
        // Present
        [blockSelf.navigationController pushViewController:browser animated:YES];
        browser.displayActionButton = NO;
        browser.displayNavArrows = NO;
        browser.displaySelectionButtons = NO;
        browser.zoomPhotosToFill = YES;
        browser.alwaysShowControls = NO;
        browser.enableGrid = YES;
        browser.startOnGrid = NO;
        browser.autoPlayOnAppear = NO;
        int num=[[imgDic objectForKey:@"imgTag"]integerValue];
        [browser setCurrentPhotoIndex:num];
        
        UIView *backView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
        [backView bk_whenTapped:^{
            [blockself.navigationController popViewControllerAnimated:YES];
        }];
        
        UIImage *icoBack = [UIImage imageNamed:@"Ico_nar_backWhite"];
        UIImageView *imgBack = [[UIImageView alloc] initWithFrame:CGRectMake(0, (40-icoBack.size.height)/2, icoBack.size.width, icoBack.size.height)];
        imgBack.image=icoBack;
        imgBack.userInteractionEnabled=YES;
        [backView addSubview:imgBack];
        
        UILabel *backTitle = [[UILabel alloc] init];
        backTitle.frame=CGRectMake(CGRectGetMaxX(imgBack.frame) + 5, 0, 50 - imgBack.frame.size.width,40);
        
        [backTitle setText:@"返回"];
        [backTitle setTextColor:[UIColor whiteColor]];
        [backTitle setFont:F(14)];
        [backView addSubview:backTitle];
        UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backView];
        browser.navigationItem.leftBarButtonItem = leftBarButtonItem;
        
        

    }];
    
}


- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
    
    return _photosAr.count;
}

- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
    if (index < _photosAr.count) {
        return [_photosAr objectAtIndex:index];
    }
    return nil;
}
//自定义标题

- (NSString *)photoBrowser:(MWPhotoBrowser *)photoBrowser

      titleForPhotoAtIndex:(NSUInteger)index {
    
    return @"查看图片";
    
}
#pragma mark 获取视频首帧图片
-(UIImage*)cutMovieImageWith:(NSURL*)urlString{
    //创建URL
    NSURL *url=urlString;
    //根据url创建AVURLAsset
    AVURLAsset *urlAsset=[AVURLAsset assetWithURL:url];
    //根据AVURLAsset创建AVAssetImageGenerator
    AVAssetImageGenerator *imageGenerator=[AVAssetImageGenerator assetImageGeneratorWithAsset:urlAsset];
    imageGenerator.appliesPreferredTrackTransform = YES;
    /*截图
     * requestTime:缩略图创建时间
     * actualTime:缩略图实际生成的时间
     */
    NSError *error=nil;
    CMTime time=CMTimeMakeWithSeconds(1, 1);//CMTime是表示电影时间信息的结构体，第一个参数表示是视频第几秒，第二个参数表示每秒帧数.(如果要活的某一秒的第几帧可以使用CMTimeMake方法)
    CMTime actualTime;
    CGImageRef cgImage= [imageGenerator copyCGImageAtTime:time actualTime:&actualTime error:&error];
    if(error){
        //        DSLog(@"截取视频缩略图时发生错误，错误信息：%@",error.localizedDescription);
        return nil;
    }
    CMTimeShow(actualTime);
    UIImage *cutImage=[UIImage imageWithCGImage:cgImage];//转化为UIImage
    return cutImage;
}
#pragma mark 定位
/**
 *用户位置更新后，会调用此函数
 *@param userLocation 新的用户位置
 */
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    //关闭地位
    
    
    [self onClickReverseGeocode];
    [_locService stopUserLocationService];
}
-(void)onClickReverseGeocode  //发送反编码请求的.
{
    isGeoSearch = false;
    CLLocationCoordinate2D pt = (CLLocationCoordinate2D){0, 0};//初始化
    if (_locService.userLocation.location.coordinate.longitude!= 0
        && _locService.userLocation.location.coordinate.latitude!= 0) {
        //如果还没有给pt赋值,那就将当前的经纬度赋值给pt
        pt = (CLLocationCoordinate2D){_locService.userLocation.location.coordinate.latitude,
            _locService.userLocation.location.coordinate.longitude
        };
    }
    BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];//初始化反编码请求
    reverseGeocodeSearchOption.reverseGeoPoint = pt;//设置反编码的店为pt
    BOOL flag = [_geocodesearch reverseGeoCode:reverseGeocodeSearchOption];//发送反编码请求.并返回是否成功
    if(flag)
    {
        echo(@"反geo检索发送成功");
    }
    else
    {
        echo(@"反geo检索发送失败");
    }
}
-(void) onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
    
    
    for (int i = 0; i < result.poiList.count; i++) {
        
        BMKPoiInfo* poi = [result.poiList objectAtIndex:i];
        if (i==0) {
            latitude= poi.pt.latitude;
            longitude = poi.pt.longitude;
            
        }
    }
}
@end
